# -*- coding: utf-8 -*-

"""
Module implementing CreateDialog.
"""

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature

from Ui_CreateDlg import Ui_CreateDialog

import general

class CreateDialog(QDialog, Ui_CreateDialog):
    """
    创建游戏对话框
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
        
        self.mGame=None
        self.playerCount=None
        self.cardPack=None
        self.cardBase=None
        self.netPlayerCount=None
        self.host=''
        self.port=1234
        
        self.lineHost.setText('0.0.0.0')

        self.showGames()
    
    def showGames(self):
        '''获取可用的游戏模块并显示到游戏列表'''
        gameNames=[m.NAME for m in general.gameModules]
        
        self.listGame.clear()
        self.listGame.addItems(gameNames)
        self.listGame.setCurrentRow(0)
        

    @pyqtSignature("int")
    def on_listGame_currentRowChanged(self, currentRow):
        """
        槽，游戏列表所选改变
        """
        self.mGame=general.gameModules[currentRow]
        player_cards=self.mGame.CONF['player_cards']
        playerCounts=list(set([str(p) for p, c in player_cards]))
        playerCounts.sort()

        self.cbPlayerCount.clear()
        self.cbPlayerCount.addItems(playerCounts)
        self.cbPlayerCount.setCurrentIndex(0)
        
        cardBases=self.mGame.CONF['cardBases']
        cardBases=[str(bc) for bc in cardBases]
        self.cbCardBase.clear()
        self.cbCardBase.addItems(cardBases)
        self.cbCardBase.setCurrentIndex(0)
        
    @pyqtSignature("int")
    def on_cbPlayerCount_currentIndexChanged(self, index):
        """
        槽，玩家总数列表所选改变
        """
        s=self.cbPlayerCount.currentText()
        if not s:
            #若是clear发出的信号，s为空，应返回
            return
        self.playerCount=int(s)
        player_cards=self.mGame.CONF['player_cards']
        cardPacks=[]
        for p, c in player_cards:
            if p==self.playerCount:
                cardPacks.append(str(c))
        cardPacks.sort()
        
        self.sbNetPlayerCount.setRange(0, self.playerCount-1)
        
        self.cbCardPack.clear()
        self.cbCardPack.addItems(cardPacks)
        self.cbCardPack.setCurrentIndex(0)

    def accept(self):
        '''槽，确定按钮的响应'''
        self.host=str(self.lineHost.text())
        self.port=self.sbPort.value()
        self.cardPack=int(self.cbCardPack.currentText())
        self.netPlayerCount=self.sbNetPlayerCount.value()
        self.cardBase=int(self.cbCardBase.currentText())
#        print self.mGame, self.host, self.port, self.cardPack, self.playerCount, self.netPlayerCount
        QDialog.accept(self)

if __name__ == '__main__':
    import sys
    from PyQt4 import QtGui
    app = QtGui.QApplication(sys.argv)
    general.updateGameModules()
    ui = CreateDialog()
    ui.show()
    sys.exit(app.exec_())
